package com.smile.mybatis;

import java.io.IOException;
import java.nio.file.*;
import java.util.ArrayList;
import java.util.List;

public class ClearErrorSql {

    public static void clearerrorsql(String inputFilePath, String outputFilePath) throws IOException {
        Path path = Paths.get(inputFilePath);
        if (!Files.exists(path)) {
            System.err.println("文件不存在: " + inputFilePath);
            return;
        }

        ArrayList<String> results = new ArrayList<>();

        // 读取输入文件
        List<String> readAllLines = Files.readAllLines(path);
        for (int i = 0; i < readAllLines.size(); i++) {

            if (readAllLines.get(i).contains("ALTER TABLE")) {
                String orgSql = readAllLines.get(i);

                String newSql = orgSql.replaceAll("\\[ERR\\] 544> ", "");

                newSql = newSql.replaceAll("WITH \\(fillfactor=0\\)", "");
                // 确保语句以分号结尾
                if (!newSql.trim().endsWith(";")) {
                    newSql = newSql + ";";
                }
                results.add(newSql);
            }
        }

        Files.write(Paths.get(outputFilePath), results,
                StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
        System.out.println("处理完成，共生成 " + results.size() + " 个表的序列代码");










    }

    public static void main(String[] args) {
        try {
            // 使用示例：输入文件路径、输出文件路径、目标模式名
            clearerrorsql("C:\\Users\\14002\\Desktop\\k高斯.txt", "C:\\Users\\14002\\Desktop\\k高斯ID.txt");
        } catch (IOException e) {
            System.err.println("文件处理错误: " + e.getMessage());
            e.printStackTrace();
        }
    }

}
